function open_link()
  local cursor = vim.api.nvim_win_get_cursor(0)
  local col = cursor[2]
  local cur = vim.api.nvim_get_current_line()
  local startPos = col + 1;
  local endPos = col + 1;
  while startPos >= 1 and string.sub(cur, startPos, startPos) ~= ' ' do
    startPos = startPos - 1
  end
  while endPos <= string.len(cur) and string.sub(cur, endPos, endPos) ~= ' ' do
    endPos = endPos + 1
  end
  local word = string.sub(cur, startPos + 1, endPos - 1)
  if string.find(word, "^(https://%S+)$") or string.find(word, "^(http://%S+)$") then
    vim.system({ 'open', word })
  end
end

vim.api.nvim_create_autocmd('BufEnter', {
  pattern = "*.md",
  callback = function()
    vim.print 'enter markdown mode'
    vim.keymap.set('n', '<cr>', open_link, { desc = 'open link' })
    vim.keymap.set('n', '<leader>t', function()
      local line = vim.api.nvim_get_current_line()
      -- vim.print(line)
      if string.find(line, '- %[x%] ') then
        local repl = string.gsub(line, '- %[x%] (%w+)', '%1', 1)
        vim.api.nvim_set_current_line(repl)
      elseif string.find(line, '- %[ %] ') then
        local repl = string.gsub(line, '- %[ %] (%w+)', '- [x] %1', 1)
        vim.api.nvim_set_current_line(repl)
      else
        vim.api.nvim_set_current_line('- [ ] ' .. line)
      end
    end, { desc = 'toggle todo' })
  end
})


local isOpenOrg = false
vim.keymap.set("n", "<leader>os", function()
  isOpenOrg = not isOpenOrg
  if isOpenOrg then
    vim.cmd('cd ' .. '~/org')
    vim.cmd('enew')
  else
    vim.cmd('bufdo bd')
    vim.cmd('cd -')
  end
  vim.cmd('NvimTreeOpen')
end, { desc = 'toggle orgmode project' })
return {
  {
    "iamcco/markdown-preview.nvim",
    cmd = { "MarkdownPreviewToggle", "MarkdownPreview", "MarkdownPreviewStop" },
    build = "cd app && yarn install",
    init = function()
      vim.g.mkdp_filetypes = { "markdown" }
    end,
    ft = { "markdown" },
  },
  {
    "Kicamon/markdown-table-mode.nvim",
    opts = true,
  },
  {
    'nvim-orgmode/orgmode',
    event = 'VeryLazy',
    ft = { 'org' },
    config = function()
      -- Setup orgmode
      require('orgmode').setup({
        org_agenda_files = '~/org/**/*',
        org_default_notes_file = '~/org/refile.org',
      })

      -- NOTE: If you are using nvim-treesitter with ~ensure_installed = "all"~ option
      -- add ~org~ to ignore_install
      -- require('nvim-treesitter.configs').setup({
      --   ensure_installed = 'all',
      --   ignore_install = { 'org' },
      -- })
    end,
  }
}
